// ajax function
var oriAramisPrice=0.0;
var tabDataState = new TabDataState();

// to know the data loaded or not
function TabDataState()
{
    this._loadStatsChartState = false;
}

$(document).ready(
    function() {
//        alert("loaded.");

//        // bind the events between functions and components
        bindEvents();
        initDatePicker();

        // call StatsChart when enter this page
//        getStatsChart();
    }
    );

function initDatePicker()
{
//        $.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['fr']))
        $("#form_date_start").datepicker({showOn: 'button', buttonImage: '/images/js_icons/calendar.gif',changeMonth: true,changeYear: true});
        $("#form_date_end").datepicker({showOn: 'button', buttonImage: '/images/js_icons/calendar.gif',changeMonth: true,changeYear: true});

//        alert(Date.parse("06/25/2009"));
        $("#form_date_start").datepicker( 'setDate' , new Date(Date.parse($('#form_date_start').val())));
        $('#form_date_start').datepicker('option', {dateFormat: 'mm/dd/yy'});

        $("#form_date_end").datepicker( 'setDate' , new Date(Date.parse($('#form_date_end').val())));
        $('#form_date_end').datepicker('option', {dateFormat: 'mm/dd/yy'});
}

function bindEvents()
{
    $('#btn_stats').bind(
        'click',
        function (e)
        {
            getStatsChart();
        }
        );

//    $("#start_day, #start_month, #start_year, #end_day, #end_month, #end_year").bind(
//        'change',
//        function (e)
//        {
//            getStatsChart();
//        }
//        );


}

function getStatsChart()
{
    var params = {};

    var startDate = $("#form_date_start").datepicker( 'getDate' );
    var endDate = $("#form_date_end").datepicker( 'getDate' );


    params.dataFormat  = 'json';
    params.partner_id  = $('#partner_id').val();
    params.start_day   = Number(startDate.getDate());
    params.start_month = Number(startDate.getMonth()+1);
    params.start_year  = Number(startDate.getFullYear());
    params.end_day     = Number(endDate.getDate());
    params.end_month   = Number(endDate.getMonth()+1);
    params.end_year    = Number(endDate.getFullYear());
//    alert(startDate +  ':' + startDate.getFullYear() + '-' + startDate.getMonth() + '-' + startDate.getDate());
//    alert(dumpObj(params,'<br/>','property:',5));

    if(params.start_year > params.end_year)
    {
        alert("start year must be less than end year");
        return false;
    }
    else
    {
//        alert(params.start_month > params.end_month);
        if((params.start_year==params.end_year) && (params.start_month > params.end_month))
        {
            alert("start time(" + params.start_month + ") must be less than end time("+ params.end_month +")");
            return false;
        }
        else
        {
            if((params.start_year==params.end_year) && (params.start_month==params.end_month) && (params.start_day > params.end_day))
            {
                alert("start time(" + params.start_day + ") must be less than end time("+ params.end_day +")");
                return false;
            }

        }
    }

   if(!tabDataState._loadStatsChartState)
    {
        $.getJSON(
            BASE_URL+"statistic/getStatsChart",
//            BASE_URL+"statistic/ajaxTest",
            params,
            function(jsonData){
                getStatsChartCallBack(jsonData);
            }
            );
    }
}

function getStatsChartCallBack(jsonData)
{
    $("#statistic_chart")[0].innerHTML = jsonData['items'][0].content;
//    $("#debug_div")[0].innerHTML = jsonData['items'][0].content;
}



